package com.project.controller;

import com.project.bean.UserBean;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;
    @RequestMapping("register")
    public String register(UserBean user){
        service.register(user);
        return "ok";
    }

    @RequestMapping("checkUser")
    public UserBean checkUser(String username){
        return service.findByName(username);
    }

    @RequestMapping("findLoginUser")
    public UserBean findLoginUser(HttpServletRequest req){
        return (UserBean) req.getSession().getAttribute("loginUser");
    }

    @RequestMapping("login")
    public UserBean login(String username, String password, HttpServletRequest req){
        UserBean user = service.login(username, password);
        req.getSession().setAttribute("loginUser",user);
        return user;
    }

    @RequestMapping("quit")
    public String quit( HttpServletRequest req){
        req.getSession().removeAttribute("loginUser");
        return "ok";
    }

}
